from django.db import models

from ..user.models import UserProfile, BaseModel


# Create your models here.
class Label(BaseModel):
    name = models.CharField(
        max_length=10,
        verbose_name='标签'
    )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '父标签'
        verbose_name_plural = verbose_name


class SonLabel(BaseModel):
    father_label = models.ForeignKey(
        Label,
        on_delete=models.CASCADE,
        verbose_name='父标签',
        related_name='father_label'
    )
    name = models.CharField(
        max_length=10,
        verbose_name='子标签'
    )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '子标签'
        verbose_name_plural = verbose_name


class LiveRoom(BaseModel):
    """直播间"""
    user = models.OneToOneField(
        UserProfile,
        on_delete=models.CASCADE,
        related_name='live_room',
        verbose_name='用户'
    )
    label = models.ForeignKey(
        SonLabel,
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
        related_name='live_room_label',
        verbose_name='标签'
    )
    name = models.CharField(
        max_length=20,
        verbose_name='房间名',
        blank=True,
        null=True
    )
    number = models.IntegerField(
        unique=True,
        auto_created=True,
        verbose_name='房间号'
    )
    image = models.ImageField(
        upload_to=r'live_room/%Y/%m',
        verbose_name='封面',
        blank=True,
        null=True
    )
    hot_num = models.IntegerField(
        default=0,
        verbose_name='人气'
    )
    is_active = models.BooleanField(
        default=False,
        verbose_name='是否有效'
    )
    is_open = models.BooleanField(
        default=False,
        verbose_name='是否开始直播'
    )
    live_stream = models.CharField(
        blank=True,
        null=True,
        max_length=50,
        verbose_name='stream'
    )
    live_key = models.CharField(
        blank=True,
        null=True,
        max_length=50,
        verbose_name='token'
    )

    def __str__(self):
        return '直播间:{}:用户:{}'.format(self.name, self.user)

    def save(self, *args, **kwargs):
        """上传时删除旧文件"""
        try:
            this = LiveRoom.objects.get(id=self.id)
            if this.image != self.image:
                this.image.delete(save=False)
        except:
            pass
        super(LiveRoom, self).save(*args, **kwargs)

    class Meta:
        verbose_name = '直播间'
        verbose_name_plural = verbose_name


class Gift(BaseModel):
    """礼物"""
    name = models.CharField(
        max_length=10,
        verbose_name='名字'
    )
    money = models.IntegerField(
        default=0,
        verbose_name='钱'
    )
    contribution = models.IntegerField(
        default=0,
        verbose_name='贡献'
    )
    info = models.CharField(
        max_length=30,
        default='',
        verbose_name='简介'
    )

    def __str__(self):
        return '礼物:{}'.format(self.name)

    class Meta:
        verbose_name = '礼物'
        verbose_name_plural = verbose_name


class ReceiveGift(BaseModel):
    """直播间接收的礼物"""
    gift = models.ForeignKey(
        Gift,
        on_delete=models.CASCADE,
        related_name='receive_gift',
        verbose_name='礼物'
    )
    live_room = models.ForeignKey(
        LiveRoom,
        on_delete=models.CASCADE,
        related_name='live_room',
        verbose_name='直播间'
    )
    num = models.IntegerField(
        default=0,
        verbose_name='数量'
    )

    def __str__(self):
        return '{room}接收到{gift}'.format(room=self.live_room, gift=self.gift)

    class Meta:
        verbose_name = '直播间接收到的礼物'
        verbose_name_plural = verbose_name


class RoomUserList(BaseModel):
    """用户打赏榜单"""
    user = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        verbose_name='打赏用户',
        related_name='gift_user'
    )
    live_room = models.ForeignKey(
        LiveRoom,
        on_delete=models.CASCADE,
        verbose_name='直播间',
        related_name='room_user'
    )
    weight = models.IntegerField(
        default=0,
        verbose_name='权重'
    )

    def __str__(self):
        return '{user}打赏{room}'.format(user=self.user, room=self.live_room.name)

    class Meta:
        verbose_name = '直播间打赏榜单'
        verbose_name_plural = verbose_name
